/* 
 -----------------------------------------------------------------------------
                   Cogaen - Component-based Game Engine (v3)
 -----------------------------------------------------------------------------
 This software is developed by the Cogaen Development Team. Please have a 
 look at our project home page for further details: http://www.cogaen.org
    
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright (c) Roman Divotkey, 2010-2011

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.cogaen.resource;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.cogaen.core.Core;
import org.cogaen.core.Service;
import org.cogaen.logging.LoggingService;

public class ResourceManager implements Service {

	public static final String NAME = "cogaen.resourcemanager";
	public static final String LOGGING_SOURCE = "ResourceManager";
	public static final String DEFAULT_GROUP = "default";
	private Map<String, ResourceHandle> resources = new HashMap<String, ResourceHandle>();
	private Map<String, List<String>> groups = new HashMap<String, List<String>>();
	private Core core;
	private LoggingService logger;
		
	public boolean hasResource(String name) {
		return this.resources.containsKey(name);
	}
	
	public Object getResource(String name) {
		ResourceHandle hndl = this.resources.get(name);
		if (hndl == null) {
			logger.logWarning(LOGGING_SOURCE, "resource '" + name + "' not found");
			return null;
		}
		
		if (!hndl.isLoaded()) {
			hndl.load(this.core);
		}
		
		return hndl.getResource();
	}

	public URL getUrl(String name) {
		URL url = getClass().getResource(name);
		if (url == null) {
			url = getClass().getResource("/" + name);
		}
		
		return url;
	}
	
	public InputStream getStream(String name) {
		InputStream is = getClass().getResourceAsStream(name);
		
		if (is == null) {
			is = getClass().getResourceAsStream("/" + name);
		}
		
		return is;
	}
	
	public void addResource(ResourceHandle hResource) {
		addResource(DEFAULT_GROUP, hResource);
	}
	
	public void addResource(String group, ResourceHandle hResource) {
		ResourceHandle hOldResource = this.resources.put(hResource.getName(), hResource);
		
		if (hOldResource != null) {
			this.resources.put(hOldResource.getName(), hOldResource);
			throw new RuntimeException("resource with name '" + hResource.getName() + "' already exists");
		}
		
		List<String> resList = groups.get(group);
		if (resList == null) {
			resList = new ArrayList<String>();
			groups.put(group, resList);
			this.logger.logDebug(LOGGING_SOURCE, "created resource group '" + group + "'");
		}
		resList.add(hResource.getName());
		
		this.logger.logDebug(LOGGING_SOURCE, "added resource '" + hResource.getName() + "' to group '" + group + "'");
	}
		
	public boolean hasGroup(String name) {
		return this.groups.containsKey(name);
	}
	
	public void loadGroup(String name) {
		List<String> resList = this.groups.get(name);
		if (resList == null) {
			this.logger.logWarning(LOGGING_SOURCE, "unable to load resource group; group '" + name + "' does not exist");
			return;
		}

		this.logger.logInfo(LOGGING_SOURCE, "loading resource group '" + name + "'");
		for (Iterator<String> it = resList.iterator(); it.hasNext(); ) {
			ResourceHandle hRes = this.resources.get(it.next());
			if (!hRes.isLoaded()) {
				hRes.load(this.core);
				this.logger.logDebug(LOGGING_SOURCE, "resource '" + hRes.getName() + "' loaded");
			}
		}
	}
	
	public void unloadGroup(String name) {
		List<String> resList = this.groups.get(name);
		if (resList == null) {
			this.logger.logWarning(LOGGING_SOURCE, "unable to unload resource group; group '" + name + "' does not exist");
			return;
		}
		
		this.logger.logInfo(LOGGING_SOURCE, "unloading resource group '" + name + "'");
		for (Iterator<String> it = resList.iterator(); it.hasNext(); ) {
			ResourceHandle hRes = this.resources.get(it.next());
			if (hRes.isLoaded()) {
				hRes.unload();
				this.logger.logDebug(LOGGING_SOURCE, "resource '" + hRes.getName() + "' unloaded");
			}
		}		
	}
	
	public static final ResourceManager getInstance(Core core) {
		return (ResourceManager) core.getService(NAME);
	}
	
	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public void initialize(Core core) {
		this.core = core;
		this.logger = LoggingService.getInstance(core);
	}
}
